package com.aiju.cash.util;

import android.content.Context;

import com.aiju.app.DataProviderManager;
import com.aiju.app.IDialogSimpleListener;
import com.aiju.app.config.ConfigManager;
import com.aiju.bean.Product;
import com.aiju.cash.core.api.DataJson;
import com.aiju.cash.core.api.ProductApi;
import com.aiju.common.updade.UpdateManager;
import com.aiju.utils.PreferencesUtils;
import com.aiju.utils.T;
import com.aiju.widgit.dialog.VersionDialog;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zhy.http.okhttp.callback.StringCallback;

import okhttp3.Call;

/**
 * @author: 佐为
 * @date: 2017-01-07 16:59
 * @version: V1.0
 * @Description: 版本检查
 */
public class CheckVersion extends UpdateManager {

    private Product product;
    private boolean toast;

    public CheckVersion(Context context) {
        super(context);
    }

    public CheckVersion(Context context, boolean toast) {
        super(context);
        this.toast = toast;
    }

    @Override
    protected void checkVersion(final IDialogSimpleListener listener) {
        new ProductApi().checkVersion("1", ConfigManager.getInstance().getUser().getToken(), new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                listener.onConfirm(null);
            }

            @Override
            public void onResponse(String response, int id) {
                DataJson dataJson = new DataJson(response);
                if (dataJson.isSuccess()) {
                    product = new Gson().fromJson(dataJson.getData().toString(), new TypeToken<Product>() {
                    }.getType());
                    if (product==null){
                        return;
                    }
                    if (!product.getVersion().equals(DataProviderManager.getDataProvider().getVersionName())) {
                        listener.onConfirm(null);
                    } else {
                        listener.onCancel(null);
                        if (toast){
                            T.ToastShort("已是最新版本");
                        }
                        PreferencesUtils.putInt(mContext, "update", 0);
                    }

                }
            }
        });
    }

    @Override
    protected void showUpdateDialog(final IDialogSimpleListener listenr) {
        if (product == null) {
            return;
        }
        new VersionDialog(mContext)
                .setTitileText(product.getDetail())
                .setOnSimpleClick(new VersionDialog.OnSimpleClick() {
                    @Override
                    public void onCancel() {
                        listenr.onCancel(null);
                        PreferencesUtils.putInt(mContext, "update", 1);
                    }

                    @Override
                    public void onSubmit() {
                        listenr.onConfirm(null);
                        PreferencesUtils.putInt(mContext, "update", 0);
                    }
                }).setLayoutParams().show();
    }

    @Override
    protected void downloadApk() {
        new ProductApi().download(product.getName(), product.getVersion(),
                product.getType(), ConfigManager.getInstance().getUser().getToken(), new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        DataJson dataJson = new DataJson(response);
                        if (dataJson.isSuccess()) {
                            // T.ToastShort("下载地址" + dataJson.getData().toString());
                            NotificationDown down = new NotificationDown(mContext);
                            down.downLoadNewApk(dataJson.getData().toString(), product.getVersion());
                        }
                    }
                });
    }

    @Override
    protected void cancelUpdate() {
        PreferencesUtils.putInt(mContext, "update", 0);
        // T.ToastShort("更新关闭");
    }
}
